//package org.hry.socket.test;
//
//import java.io.IOException;
//import java.net.InetSocketAddress;
//import java.nio.ByteBuffer;
//import java.nio.channels.*;
//import java.nio.charset.StandardCharsets;
//import java.util.Iterator;
//import java.util.Set;
//
///**
// * @Desc
// * @Author houry
// * @Date 2021/2/25 8:49
// **/
//public class NioServerSocket {
//    public static void main(String[] args) throws IOException {
//        ServerSocketChannel serverSocket = ServerSocketChannel.open();
//        serverSocket.configureBlocking(false);
//        Selector selector = Selector.open();
//        serverSocket.socket().bind(new InetSocketAddress(8888));
//        serverSocket.register(selector, SelectionKey.OP_ACCEPT);
//
//        while (true) {
//            selector.select();
//            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
//            while (iterator.hasNext()) {
//                SelectionKey key = iterator.next();
//                iterator.remove();
//                if (key.isAcceptable()) {
//                    dealWithAcceptable(key);
//                }
//                if (key.isWritable()) {
//                    dealWithWritable(key);
//                }
//                if (key.isReadable()) {
//                    dealWithReadable(key);
//                }
//            }
//        }
//    }
//
//    private static void dealWithReadable(SelectionKey key) throws IOException {
//        ByteBuffer byteBuffer = (ByteBuffer) key.attachment();
//        SocketChannel socket = (SocketChannel) key.channel();
//        ByteBuffer buffer = ByteBuffer.allocate(1024);
//        socket.read(buffer);
//        buffer.flip();
//        byteBuffer.limit(byteBuffer.capacity());
//        byteBuffer.put(buffer);
//
//    }
//
//    private static void dealWithWritable(SelectionKey key) {
//        ByteBuffer buffer=(ByteBuffer) key.attachment();
//        SocketChannel channel=(SocketChannel) key.channel();
//
//        buffer.flip();
//
//        String msg=CharSetUtil.decode(buffer, "UTF-8");
//
//        if(msg.indexOf("\r\n")==-1){
//            return;
//        }
//
//        String outPutData=msg.substring(0, msg.indexOf("\n")+1);
//        System.out.println("接收来自客户端的数据："+outPutData);
//
//        ByteBuffer outbyteBuffer=CharSetUtil.encode("echo:"+outPutData, "UTF-8");
//        while (outbyteBuffer.hasRemaining()) {
//            channel.write(outbyteBuffer);
//        }
//
//        ByteBuffer tmp=CharSetUtil.encode(outPutData, "UTF-8");
//        buffer.position(tmp.limit());
//        buffer.compact();
//        if("bye\r\n".equalsIgnoreCase(outPutData)){
//            key.cancel();
//            channel.close();
//            System.out.println("关闭与客户端的连接");
//        }
//    }
//
//    private static void dealWithAcceptable(SelectionKey key) throws IOException {
//        SocketChannel accept = ((ServerSocketChannel) key.channel()).accept();
//        accept.configureBlocking(false);
//        System.out.println("接收到客户端的请求！");
//        accept.register(key.selector(), SelectionKey.OP_READ);
//    }
//}
